package com.zhuiyun.educations.api.member.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhuiyun.educations.api.member.entity.Member;
import com.zhuiyun.educations.api.member.mapper.MemberMapper;
import com.zhuiyun.educations.api.member.service.MemberService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MemberVoServiceImpl
 * @Description TODO 会员类型管理
 * @Author ys
 * @Date 2023/6/16 8:28
 **/
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;

    /**
     * @Author ys
     * @Description TODO 分页查询
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult getMember(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest);
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("delFlag",0);
        PageResult pageResult = MybatisPageHelper.getPageResult(memberMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
    /**
     * @Author ys
     * @Description TODO 新增
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult addMember(Member member){
        Date date =  new Date();
        member.setDelFlag(0);
        member.setCreateTime(date);
        member.setUpdateTime(date);
        return CommonResult.ok(memberMapper.insertSelective(member));
    }
    /**
     * @Author ys
     * @Description TODO 修改
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult updateMember(Member member){
        Date date =  new Date();
        member.setUpdateTime(date);
        return CommonResult.ok(memberMapper.updateByPrimaryKeySelective(member));
    }
    /**
     * @Author ys
     * @Description TODO 修改状态
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult updatestate(Member member){
        Date date =  new Date();
        member.setUpdateTime(date);
        if (member.getMemberState().equals("1")) {
            member.setMemberState("1");
        } else {
            member.setMemberState("0");
        }
        return CommonResult.ok(memberMapper.updateByPrimaryKeySelective(member));
    }

    /**
     * @Author ys
     * @Description TODO 逻辑删除
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult deleteMemberById(int id){
        return CommonResult.ok(memberMapper.deleteMemberById(id));
    }
    /**
     * @Author ys
     * @Description TODO 批量逻辑删除
     * @Date 2023/6/26 14:09
     * @Return CommonResult
     * @param
     */
    @Override
    public CommonResult deleteMemberByIds(List<String> ids){
        return CommonResult.ok(memberMapper.deleteMemberByIds(ids));
    }
}
